from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

from .utils import get_current_user_credentials


class OAuth2RequiredMixin(object):

    def dispatch(self, request, *args, **kwargs):
        self.request = request
        self.args = args
        self.kwargs = kwargs

        credentials = get_current_user_credentials()

        if not credentials:
            return HttpResponseRedirect(reverse("login"))

        return super(OAuth2RequiredMixin, self).dispatch(
            request, *args, **kwargs)
